package com.aiwei.tdjk.service.impl;

import com.aiwei.tdjk.bean.Pager;
import com.aiwei.tdjk.dao.extend.PermissionExtendMapper;
import com.aiwei.tdjk.dto.request.GetPermissionPagerRequest;
import com.aiwei.tdjk.entity.Permission;
import com.aiwei.tdjk.entity.PermissionExample;
import com.aiwei.tdjk.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by yanfeng on 2016/12/13.
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionExtendMapper permissionExtendMapper;

    @Override
    public Permission get(Long id) {
        return permissionExtendMapper.selectByPrimaryKey(id);
    }

    @Override
    public void save(Permission permission) {
        permissionExtendMapper.insertSelective(permission);
    }

    @Override
    public int update(Permission permission) {
        return permissionExtendMapper.updateByPrimaryKey(permission);
    }

    @Override
    public int delete(Long id) {
        return permissionExtendMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int delete(Long[] ids) {
        if (ids!=null&&ids.length>0){
            for (Long id:ids){
                delete(id);
            }
            return ids.length;
        }
        return 0;
    }

    @Override
    public List<Permission> list() {
        return permissionExtendMapper.selectByExample(new PermissionExample());
    }

    /**
     * 获取分页列表
     *
     * @param request
     */
    @Override
    public Pager<Permission> find(GetPermissionPagerRequest request) {
        request.webParamConvert();
        List<Permission> PermissionList = permissionExtendMapper.find(request);
        int totalCount = permissionExtendMapper.getTotalCount(request);
        Pager<Permission> pager = new Pager<>();
        pager.setPageNumber(request.getPageNumber());
        pager.setPageSize(request.getPageSize());
        pager.setList(PermissionList);
        pager.setTotalCount(totalCount);
        return pager;
    }
}
